package com.usefullc.extinguisher.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.constants.DataConfigConstant;
import com.usefullc.common.context.ContextManager;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.common.util.NumberUtil;
import com.usefullc.extinguisher.domain.FireDevice;
import com.usefullc.extinguisher.domain.FireDeviceAuditRecord;
import com.usefullc.extinguisher.param.FireDeviceAuditRecordParam;
import com.usefullc.extinguisher.query.FireDeviceAuditRecordQuery;
import com.usefullc.extinguisher.service.FireDeviceAuditRecordService;
import com.usefullc.extinguisher.service.FireDeviceService;
import com.usefullc.extinguisher.vo.FireDeviceAuditRecordVo;
import com.usefullc.extinguisher.vo.FireDeviceVo;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.domain.DataConfig;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.service.DataConfigService;
import com.usefullc.system.validator.UsefulValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.List;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 设备检查作业记录控制类
 * @author: 星空
 * @date: 2022-09-08
 */
@Controller
@RequestMapping("/")
@Api( tags="消防设备web")
public class FireIndexController extends BaseController {

    @Autowired
    private FireDeviceService fireDeviceService;

    @Autowired
    private DataConfigService dataConfigService;

    @SneakyThrows
    @GetMapping("detail.php")
    public String deviceDetail( Model model, HttpServletRequest request) {
//        String code = request.getParameter("code");
//        code = URLDecoder.decode(code, Charset.defaultCharset().toString());
//        //由于历史原因，tenantId 不能通过参数传递，这里通过配置中获取
//        String autoBindTenant = dataConfigService.findValueByCode(DataConfigConstant.AUTO_BIND_TENANT);
//        if (BooleanUtil.toBoolean(autoBindTenant)) {
//            String autoBindTenantId = dataConfigService.findValueByCode(DataConfigConstant.AUTO_BIND_TENANT_ID);
//            ContextManager.setTenantId(NumberUtil.getNumber(autoBindTenantId));
//        }
//        FireDeviceVo fireDeviceVo = fireDeviceService.findFullByCode(code);
//        ContextManager.clean();
//        model.addAttribute("entity",fireDeviceVo);
        return "public/detail.html";
    }



    @SneakyThrows
    @ResponseBody
    @GetMapping("findDeviceDetailByCode")
    public WebResult findDeviceDetailByCode( Model model, HttpServletRequest request) {
        String code = request.getParameter("code");
        code = URLDecoder.decode(code, Charset.defaultCharset().toString());
        code = URLDecoder.decode(code, Charset.defaultCharset().toString());
        //由于历史原因，tenantId 不能通过参数传递，这里通过配置中获取
        String autoBindTenant = dataConfigService.findValueByCode(DataConfigConstant.AUTO_BIND_TENANT);
        if (BooleanUtil.toBoolean(autoBindTenant)) {
            String autoBindTenantId = dataConfigService.findValueByCode(DataConfigConstant.AUTO_BIND_TENANT_ID);
            ContextManager.setTenantId(NumberUtil.getNumber(autoBindTenantId));
        }
        FireDeviceVo fireDeviceVo = fireDeviceService.findFullByCode(code);
        ContextManager.clean();
        return WebResultBuilder.successResult(fireDeviceVo);
    }







}
